package br.com.bobo.main;

import java.io.IOException;
import javax.microedition.lcdui.Image;
import javax.microedition.lcdui.game.Sprite;

/**
 *
 * @author leandro
 */
public class Bobo extends Sprite {


    //frame sequences
    private int[] runningSeq = {3,4,5};
    private int[] stopSeq = null;
    private int[] jumpingSeq = {6,7};
    private int[] ladderSeq = {1,2};
    private int[] fallingSeq = {8};

    //states
    private boolean stoped = true;
    private boolean inLadder = false;
    private boolean falling = false;

    //movements
    private int direction = TRANS_NONE;
    private int speed = 5;
    private int gravity = 10;
    public int maxJumpHeight;
    public int currentJumpHeight;

    private long startJump;

    public Bobo(Image img,int frameW, int frameH) {
        super(img, frameW, frameH);
    }

    public static Bobo getInstance(){
        Image img;
        Bobo bobo;

        try{
            img = Image.createImage("/bobo_28x33.png");
            int frameW = 28;
            int frameH = 33;

        bobo = new Bobo(img,frameW,frameH);
        bobo.defineReferencePixel(frameW/2, frameH/2);

        bobo.setPosition(0, 70);

        return bobo;

        }catch(IOException ioe){
          ioe.printStackTrace();

        }

        return null;
    }

    public void update(long time){
        move();

        if(falling){
            setFrameSequence(fallingSeq);
            super.move(0,gravity);
        }else{
            if(maxJumpHeight != 0){
                if(++currentJumpHeight <= maxJumpHeight ){
                    super.move(0,-1*gravity);
                    if(getFrame() < 6)
                        setFrame(5);
                    if(getFrame() > 7 )
                        setFrame(7);
                    nextFrame();
                    try{
                        Thread.sleep(10);
                    }catch(Exception e){}
                    
                }
                else{
                    currentJumpHeight = 0;
                    maxJumpHeight = 0;
                    falling = true;
                }
            }
        }
    }


    public void startJump(long start){

        if(startJump == 0){
            startJump = start;
        }

    }

    public void stopJump(long stop){

        long currentTimeJump = stop - startJump;
        int dir = (direction == TRANS_MIRROR ? -1 : (direction == TRANS_NONE ? 1 : 0));
        setTransform(direction);
        if(!falling){
            if(currentTimeJump <= 400){
                maxJumpHeight = 3;
            }
            if(currentTimeJump > 400 && currentTimeJump <= 1000){
                maxJumpHeight = 5;
            }
            if(currentTimeJump > 1000){
                maxJumpHeight = 8;
            }
        }
        startJump = 0;

    }

    public void move(){
        int velocity = 0;
        if(!stoped){
            int dir = (direction == TRANS_MIRROR ? -1 : (direction == TRANS_NONE ? 1 : 0));
            setTransform(direction);
            
            velocity = dir * speed;
            nextFrame();
            super.move(velocity,0);
        }else{
            setFrameSequence(stopSeq);
            velocity = 0;
        }
   }

    public boolean isInLadder(){
        return inLadder;
    }

    public void setInLadder(boolean ladder){
        inLadder = ladder;
    }


    public void setDirection(int dir){
        direction = dir;
    }

    public int getDirection(){
        return direction;
    }

    public void setStoped(boolean stoped){
        this.stoped = stoped;
    }

    public boolean isStoped(){
        return stoped;
    }

    public void setInTheAir(boolean air){
        falling = air;
    }

    public boolean isInTheAir(){
        return falling;
    }


    public int[] getRunningSeq(){
        return runningSeq;
    }

    public int[] getJumpingSeq(){
        return jumpingSeq;
    }

}
